package com.hh.base.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.hh.base.util.ext.getGenericsClass
import com.hh.base.widget.dialog.MultifunctionDialog
import com.hh.base.widget.pickerview.listener.OnTimeSelectListener
import java.util.Date

/**
 * Create By hHui on 2025/4/29 10:14
 *
 * @description Fragment基类
 */
abstract class BaseFragment<T : BaseViewModel, R : ViewDataBinding> : Fragment(), BaseView {
    val TAG = this.javaClass.simpleName
    lateinit var viewModel: T
    lateinit var dataBinding: R
    lateinit var baseActivity: BaseActivity<*, *>

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        viewModel = ViewModelProvider(this)[this.getGenericsClass(0)]
        dataBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false)
        dataBinding.lifecycleOwner = this
        this.lifecycle.addObserver(viewModel)
        val kClass = dataBinding::class.java
        val method = kClass.getMethod("setViewModel", this.getGenericsClass(0))
        method.invoke(dataBinding, viewModel)
        return dataBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        baseActivity = activity as BaseActivity<*, *>
        initObserve()
    }


    override fun onDestroy() {
        super.onDestroy()
        this.lifecycle.removeObserver(viewModel)
    }

    @LayoutRes
    abstract fun getLayoutId(): Int


    override fun showToast(msg: String) {
        baseActivity.showToast(msg)
    }

    override fun alertMessage(msg: String) {
        baseActivity.alertMessage(msg)
    }

    override fun alertTitleAndMessage(title: String, msg: String) {
        baseActivity.alertTitleAndMessage(title, msg)
    }

    override fun showLoading(msg: String) {
        baseActivity.showLoading(msg)
    }

    override fun hideLoading() {
        baseActivity.hideLoading()
    }

    override fun showLoadingCancel(msg: String, cancelText: String, cancelListener: MultifunctionDialog.AlertCancelListener) {
        baseActivity.showLoadingCancel(msg, cancelText, cancelListener)
    }


    override fun alertOption(title: String, msg: String, clickListener: MultifunctionDialog.AlertClickListener, sureText: String, cancelText: String) {
        baseActivity.alertOption(title, msg, clickListener, sureText, cancelText)
    }

    override fun jump2Activity(cla: Class<*>, isFinishBefore: Boolean, map: Map<String, String>?) {
        baseActivity.jump2Activity(cla, isFinishBefore, map)
    }

    override fun showDateTimerPicker(callback: OnTimeSelectListener, date: Date?, array: BooleanArray) {
        baseActivity.showDateTimerPicker(callback, date, array)
    }


    private fun initObserve() {
        viewModel.toastMessage.observe(viewLifecycleOwner) {
            it.getContentIfNotHandled()?.let { msg ->
                showToast(msg)
            }
        }
        viewModel.alertMessage.observe(viewLifecycleOwner) {
            it.getContentIfNotHandled()?.let { msg ->
                alertMessage(msg)
            }
        }
        viewModel.alertTitleAndMessage.observe(viewLifecycleOwner) {
            it.getContentIfNotHandled()?.let { (title, msg) ->
                alertTitleAndMessage(title, msg)
            }
        }
        viewModel.loadingMessage.observe(viewLifecycleOwner) {
            it.getContentIfNotHandled()?.let { msg ->
                showLoading(msg)
            }
        }
        viewModel.hideLoading.observe(viewLifecycleOwner) {
            it.getContentIfNotHandled()?.let {
                hideLoading()
            }
        }
        viewModel.loadingCancel.observe(viewLifecycleOwner) {
            it.getContentIfNotHandled()?.let { (msg, cancelText, listener) ->
                showLoadingCancel(msg, cancelText, listener)
            }
        }
        viewModel.alertOption.observe(viewLifecycleOwner) {
            it.getContentIfNotHandled()?.let { (title, msg, clickListener, sureText, cancelText) ->
                alertOption(title, msg, clickListener, sureText, cancelText)
            }
        }
        viewModel.jump2Activity.observe(viewLifecycleOwner) {
            it.getContentIfNotHandled()?.let { (cla, isFinishBefore, map) ->
                jump2Activity(cla, isFinishBefore, map)
            }
        }
        viewModel.showDateTimePicker.observe(viewLifecycleOwner) {
            it.getContentIfNotHandled()?.let { (date, listener, array) ->
                showDateTimerPicker(listener, date, array)
            }
        }
    }

}